package dhm.com.dhmshop.updata.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.mine.CollectionListAdapter;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.Bean;
import dhm.com.dhmshop.entity.GetCollectionList;
import dhm.com.dhmshop.utils.SpUtils;

public class CollectiongoodsActivity extends BaseActiity implements LoginContract.IView {


    @BindView(R.id.recy)
    RecyclerView recy;
    private CollectionListAdapter collectionListAdapter;

    private PressenterImpl pressenter;
    private String uid;
    private GetCollectionList getCollectionList;

    @Override
    protected int getLayout() {
        return R.layout.activity_collectiongoods;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recy.setLayoutManager(manager);
        uid = SpUtils.getString(CollectiongoodsActivity.this, "uid");
        pressenter = new PressenterImpl();
        pressenter.attachView(this);

        collectionListAdapter = new CollectionListAdapter(this);
        collectionListAdapter.setdeleteClick(new CollectionListAdapter.OnClick() {
            @Override
            public void item(int posttion) {
                Map<String,String> map=new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("uid",uid);
                map.put("type","0");
                map.put("cid",getCollectionList.getData().get(posttion).getFavoriteId()+"");
                pressenter.sendMessage(CollectiongoodsActivity.this,Constant.DelCollect,map, Bean.class);
            }
        });
        recy.setAdapter(collectionListAdapter);
    }

    @Override
    protected void initData() {
        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("uid", uid);
        pressenter.sendMessage(CollectiongoodsActivity.this, Constant.CollectionList, map, GetCollectionList.class);

    }


    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
                default:
        }
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof GetCollectionList) {
            getCollectionList = (GetCollectionList) data;
            if (getCollectionList.getCode() == 1) {
                collectionListAdapter.setShopList(getCollectionList.getData());
            }else {
                collectionListAdapter.clearShopList();
            }
        }else if (data instanceof Bean){
            Bean bean= (Bean) data;
            Toast.makeText(CollectiongoodsActivity.this,bean.getMessage() , Toast.LENGTH_SHORT).show();
            initData();
        }
    }

    @Override
    public void fail(String error) {

    }
}
